package com.zizy.healthexpert.Activity;

import android.content.Entity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.MyDataBase;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS;
import com.zizy.healthexpert.Database.MyDatabase_Dishes;
import com.zizy.healthexpert.R;

import android.support.v7.app.ActionBarActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class FoodDetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        /*find views*/
        TextView txtDishesName = (TextView) findViewById(R.id.txtDishesName);
        TextView txtMainIngredient = (TextView) findViewById(R.id.txtMainIngredient);
//        TextView txtMainNutrition = (TextView) findViewById(R.id.txtMainNutrition);
        TextView txtMinorIngredient = (TextView) findViewById(R.id.txtMinorIngredient);
        TextView txtSeasoning = (TextView) findViewById(R.id.txtSeasoning);
        TextView txtEffect = (TextView) findViewById(R.id.txtEffect);
        TextView txtSteps = (TextView) findViewById(R.id.txtSteps);
        TextView txtTip = (TextView) findViewById(R.id.txtTip);
        ImageView imgIsGoodToEat = (ImageView) findViewById(R.id.imgIsGoodToEat);

        /*Get foodID from intent*/
        int foodID = getIntent().getIntExtra("foodID", 100);
        /*define variable*/

        int isGoodToEat=Config.NOT_BAD_TO_EAT;
        String dishesName = "";
        String mainIngredient = "";
        String minorIngredient = "";
        String seasoning = "";
        String effect = "";
        String steps = "";
        String tip = "";
        /*is good to eat*/
        Bundle bundle = getIntent().getExtras();
        isGoodToEat = bundle.getInt("isGoodToEat", Config.NOT_BAD_TO_EAT);
        switch (isGoodToEat){
            case Config.NOT_BAD_TO_EAT:
                imgIsGoodToEat.setImageResource(R.drawable.search_detail_activity_able);
                break;
            case Config.GOOD_TO_EAT:
                imgIsGoodToEat.setImageResource(R.drawable.search_detail_activity_suit);
                break;
            case Config.CAN_NOT_EAT:
                imgIsGoodToEat.setImageResource(R.drawable.search_detail_activity_forbit);
                break;
        }
        if (foodID == Config.QR_ID) { /*從QR—Code來的食物*/


            /*dishesName*/
            dishesName = bundle.getString(Config.bundle.DISHES_NAME);
            String mainIngredientFormatted = bundle.getString(Config.bundle.MAIN_INGREDIENT);
            ArrayList<String> mainIngredientList = new ArrayList<String>();
            String[] mainIngredientArr = mainIngredientFormatted.split(";");
            for (String ing : mainIngredientArr) { //->    id - > chinese
                String[] tArr = ing.split(",");
                if (tArr[0] == "" || tArr == null) break;
                int tempID = Integer.parseInt(tArr[0]);
                MyDataBase_T_FOODS dataBase_t_foods = new MyDataBase_T_FOODS(getApplicationContext());
                String tempFoodName = ((ArrayList<MyDataBase_T_FOODS.T_FOODS>) dataBase_t_foods.SearchT_FOODS("*", String.format(("_ID = %d"), tempID))).get(0).NAME;
                mainIngredientList.add(tempFoodName);
                dataBase_t_foods.Close();
            }
            /*Main Ingredient*/
            for (Iterator iterator = mainIngredientList.iterator(); iterator.hasNext(); ) {
                mainIngredient += iterator.next().toString()+";\t";
            }

        } else {
            /*prepare database*/
            MyDatabase_Dishes database_dishes = new MyDatabase_Dishes(getBaseContext());
            /*search the id and get a only result*/
            ArrayList<Object> searchedDishesObjectList = database_dishes.SearchDatabase("*", String.format("dishesID = %d", foodID));
            MyDatabase_Dishes.FieldsDishes dishes = (MyDatabase_Dishes.FieldsDishes) searchedDishesObjectList.get(0);

            /*set value*/
            dishesName = dishes.dishesName;
            if (dishes.mainIngredient!=null) mainIngredient = dishes.mainIngredient.replace(";","    ");
            if (dishes.minorIngredient!=null) minorIngredient = dishes.minorIngredient.replace(";","    ");
            if (dishes.seasoning!=null) seasoning = dishes.seasoning.replace(";","    ");
            if (dishes.effect!=null) effect = dishes.effect;
            if (dishes.steps!=null)steps = dishes.steps.replace(";", "\n").replace("；","\n").replace("。","。\n");
            if (dishes.tips!=null)  tip = dishes.tips.replace(";","    ");

        }
        /*set txtViews*/
        txtDishesName.setText(dishesName);
        txtMainIngredient.setText(mainIngredient);
        txtMinorIngredient.setText(minorIngredient);
        txtSeasoning.setText(seasoning);
        txtEffect.setText(effect);
        txtSteps.setText(steps);
        txtTip.setText(tip);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.food_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
